package com.wps.wdztv6.config;


import com.wps.wdztv6.common.R;
import com.wps.wdztv6.common.ResultEnum;
import com.wps.wdztv6.entity.ThirdFile;
import com.wps.wdztv6.service.FileService;
import com.wps.wdztv6.utils.WPS4Util;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.File;


@Component
@Log4j2
public class InitApplication implements CommandLineRunner {

    @Autowired
    FileService fileService;

    @Value("${wdztapp.ak}")
    String ak;

    @Value("${wdztapp.sk}")
    String sk;

    @Value("${wdztapp.host}")
    String host;

    @Value("${upload.file.savepath}")
    String uploadPath;

    @Value("${myserver.api}")
    String serverApi;

    @Override
    public void run(String... args) {
        log.info(">>>>>>>>>>>>>>> application init start <<<<<<<<<<<<<");
        fileService.resetFile();

        for (String type : WdztAppInfoConfig.templateTypes){
            R<ThirdFile> result = fileService.createTemplateFile(type);
            if (result.getResult() == ResultEnum.SUCCESS.getCode()) {
                log.info("创建模版数据:{} {} 成功",result.getData().getId(),result.getData().getName());
            } else {
                log.error("创建模版数据:{} {}",type,result.getMessage());
            }
        }
        log.info("初始化wdztapp信息");
        log.info("host: {}",host);
        WdztAppInfoConfig.host = host;
        log.info("ak: {}",ak);
        WdztAppInfoConfig.ak = ak;
        log.info("sk: {}",sk);
        WdztAppInfoConfig.sk = sk;

        WPS4Util.initAppInfo(ak,sk);

        log.info("设置的文件上传路径:{}",uploadPath);
        File folder = new File(uploadPath);
        log.info("真实的上传路径:{}",folder.getAbsolutePath());
        if(!folder.isDirectory()){
            log.info("上传路径不存在，创建");
            boolean result = folder.mkdirs();
            if (result) {
                log.info("上传路径创建成功");
            } else {
                log.error("上传路径创建失败");
                System.exit(500);
            }
        }
        log.info(">>>>>>>>>>>>>>> application init end <<<<<<<<<<<<<");

        log.info("在线编辑 Word url: {}{}",serverApi,
                String.format("/open/api/edit/link?file_id=%s", WdztAppInfoConfig.templateWordId));
        log.info("在线编辑 Excel url: {}{}",serverApi,
                String.format("/open/api/edit/link?file_id=%s", WdztAppInfoConfig.templateExcelId));
        log.info("在线编辑 PPT url: {}{}",serverApi,
                String.format("/open/api/edit/link?file_id=%s", WdztAppInfoConfig.templatePptId));
        log.info("在线编辑 pdf url: {}{}",serverApi,
                String.format("/open/api/edit/link?file_id=%s", WdztAppInfoConfig.templatePdfId));
        log.info("在线预览 Word url: {}{}",serverApi,
                String.format("/open/api/preview/link?file_id=%s", WdztAppInfoConfig.templateWordId));
        log.info("在线预览 Excel url: {}{}",serverApi,
                String.format("/open/api/preview/link?file_id=%s", WdztAppInfoConfig.templateExcelId));
        log.info("在线预览 PPT url: {}{}",serverApi,
                String.format("/open/api/preview/link?file_id=%s", WdztAppInfoConfig.templatePptId));
        log.info("在线预览 pdf url: {}{}",serverApi,
                String.format("/open/api/preview/link?file_id=%s", WdztAppInfoConfig.templatePdfId));
        log.info("在线预览 zip url: {}{}",serverApi,
                String.format("/open/api/preview/link?file_id=%s", WdztAppInfoConfig.templateZipId));
    }
}